package cn.shyouchi.wuguanjia.android.ui.home.security.thread.message.simplenotice;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.Global;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.matter.MatterApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.DataChangeEvent;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.ui.home.security.thread.message.birthday.BirthdayWish;
import cn.shyouchi.wuguanjia.android.ui.home.security.thread.message.birthday.BirthdayWishAdapter;
import cn.shyouchi.wuguanjia.android.ui.home.security.thread.message.birthday.ChooseGiftActivity;
import cn.shyouchi.wuguanjia.android.ui.home.security.thread.message.matter.MatterBirthday;
import cn.shyouchi.wuguanjia.android.utils.ToastUtil;
import cn.shyouchi.wuguanjia.android.widget.GlideCircleTransform;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class StaffBirthdayActivity extends BaseActivity {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.img_show)
    ImageView mImgShow;
    @BindView(R.id.tv_person_name)
    TextView mTvPersonName;
    @BindView(R.id.rv_gift_list)
    RecyclerView mRvGiftList;
    @BindView(R.id.btn_send_gift)
    Button mBtnSendGift;

    List<BirthdayWish> mBirthdayWishes = new ArrayList<>();
    BirthdayWishAdapter mBirthdayWishAdapter;

    MatterBirthday mMatterBirthday;
    @BindView(R.id.img_header)
    ImageView mImgHeader;


    int noticeId;

    @Override
    public int initContentView() {
        return R.layout.activity_staff_birthday;
    }

    @Override
    public void initInjector() {
        mMatterBirthday = getIntent().getParcelableExtra("birthday");
        noticeId = getIntent().getIntExtra("noticeId", 0);

        if (mMatterBirthday != null) {
            mBirthdayWishes = mMatterBirthday.getInfo();
        }


    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        mTopBar.setTitle(getTitle().toString());
        setSupportActionBar(mTopBar.getToolbar());

        EventBus.getDefault().register(this);
        mBirthdayWishAdapter = new BirthdayWishAdapter(mBirthdayWishes);


        mRvGiftList.setAdapter(mBirthdayWishAdapter);
        mRvGiftList.setLayoutManager(new LinearLayoutManager(mContext));

        fillData(mMatterBirthday);

    }


    private void fillData(MatterBirthday matterBirthday) {


        mTvPersonName.setText(matterBirthday.getName());
        loadHead(Global.BASE_URL + matterBirthday.getAvatar(), mImgHeader);
        mBirthdayWishAdapter.notifyDataSetChanged();
    }


    private void loadHead(String path, ImageView imageView) {
        Glide.with(mContext)
                .load(path)
                .transform(new GlideCircleTransform(mContext))
                .into(imageView);
    }


    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    @OnClick(R.id.btn_send_gift)
    public void onClick() {
        if (mMatterBirthday.getBlessing() == 1) {
            ToastUtil.showToast("已经送过祝福了");
        } else {
            startActivity(new Intent(mContext, ChooseGiftActivity.class)
                    .putExtra("birthday", mMatterBirthday));
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDataChange(DataChangeEvent event) {
        new MatterApi().getBirthdayDetail(String.valueOf(noticeId))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult<MatterBirthday>>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult<MatterBirthday> matterMeetBaseRequestResult) {
                        mMatterBirthday = matterMeetBaseRequestResult.getResultMessage();
                        mBirthdayWishes.clear();
                        mBirthdayWishes.addAll(mMatterBirthday.getInfo());
                        fillData(mMatterBirthday);
                    }
                });
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
